package map;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import network.User;

import objects.Enemy;
import objects.EnemyType;
import objects.Player;
import tools.Coordinate;

public class Level implements Serializable {

	private static final long serialVersionUID = -2379353316848278654L;

	private Tile[][] tiles;
	private LinkedList<Enemy> listEnemies;
	private LinkedList<Item> listItems;
	private int roomNumber;

	public Level(int width, int height, int roomNumber) {
		tiles = new Tile[width][height];
		listEnemies = new LinkedList<Enemy>();
		listItems = new LinkedList<Item>();
		this.roomNumber = roomNumber;
		// createTestRoom();
	}

	/*public void createTestRoom() {
		listEnemies.clear();
		Random rnd = new Random();
		RoomType roomType = RoomType.values()[rnd
				.nextInt(RoomType.values().length)];

		roomType = RoomType.NORMAL;
		if (rnd.nextInt(100) < 10) {
			roomType = RoomType.BOSS;
		}

		for (int x = 0; x < tiles.length; x++) {
			for (int y = 0; y < tiles[x].length; y++) {

				if (x == 9 && y == 0) {

					tiles[x][y] = new Tile(TileType.DOOR_CLOSED_UP);

				} else if (x == 9 && y == tiles[x].length - 1) {
					tiles[x][y] = new Tile(TileType.DOOR_CLOSED_DOWN);
				} else if (x == 0 || y == 0 || x == tiles.length - 1
						|| y == tiles[0].length - 1) {
					tiles[x][y] = new Tile(TileType.WALL);
				} else {
					if (roomType == RoomType.NORMAL) {
						if (rnd.nextInt(100) > 10) {
							tiles[x][y] = new Tile(TileType.GENERAL_FLOOR);
						} else if (rnd.nextBoolean()) {
							tiles[x][y] = new Tile(TileType.CARPET);
						} else {
							tiles[x][y] = new Tile(TileType.HOLE);
						}

						if (rnd.nextBoolean() && tiles[x][y].isAccessible()) {
							if (listEnemies.size() < rnd.nextInt(4) + 1) {
								listEnemies.add(new Enemy(x, y,
										EnemyType.NORMAL));
							}
						}
					} else if (roomType == RoomType.BOSS) {
						tiles[x][y] = new Tile(TileType.CARPET);
						if (rnd.nextBoolean() && tiles[x][y].isAccessible()) {
							if (listEnemies.size() < 1) {
								listEnemies
										.add(new Enemy(x, y, EnemyType.BOSS));
							}
						}
					}
				}

			}
		}

	}*/

	public Tile[][] getTiles() {
		return tiles;
	}

	public void setTiles(Tile[][] tiles) {
		this.tiles = tiles;
	}

	public LinkedList<Enemy> getListEnemies() {
		return listEnemies;
	}

	public void setListEnemies(LinkedList<Enemy> listEnemies) {
		this.listEnemies = listEnemies;
	}

	public LinkedList<User> calcRound(LinkedList<User> listUsers) {
		Random random = new Random();
		for (int i = 0; i < listEnemies.size(); i++) {

			if (random.nextBoolean()) {
				int x = 0;
				int y = 0;

					int dir = random.nextInt(4);
					if (dir == 0) {
						x++;
					} else if (dir == 1) {
						x--;
					} else if (dir == 2) {
						y++;
					} else if (dir == 3) {
						y--;
					}

					boolean canMove = true;
					if (listEnemies.get(i).getxPosition() + x >= 0
							&& listEnemies.get(i).getxPosition() + x < tiles.length
							&& listEnemies.get(i).getyPosition() + y >= 0
							&& listEnemies.get(i).getyPosition() + y < tiles[listEnemies
									.get(i).getxPosition()
									+ x].length) {
						if (tiles[listEnemies.get(i).getxPosition() + x][listEnemies
								.get(i).getyPosition()
								+ y] != null) {
							if (!tiles[listEnemies.get(i).getxPosition() + x][listEnemies
									.get(i).getyPosition()
									+ y].isAccessible()) {
									canMove = false;
								
							} else {
								for(int n = 0; n < listEnemies.size(); n++) {
									if(n != i) {
										if(listEnemies.get(n).getxPosition() == listEnemies.get(i).getxPosition()+x && listEnemies.get(n).getyPosition() == listEnemies.get(i).getyPosition()+y) {
											canMove = false;
										}
									}
								}
							}
						} else {
							canMove = false;
						}
					} else {
						canMove = false;
					}
					
					if(!canMove) {
						x = 0;
						y = 0;
					}


				
				x = listEnemies.get(i).getxPosition() + x;
				y = listEnemies.get(i).getyPosition() + y;
				

				boolean hitPlayer = false;
				for (int n = 0; n < listUsers.size(); n++) {
					if (listUsers.get(n).getPlayer().getCoord().getX() == x
							&& listUsers.get(n).getPlayer().getCoord().getY() == y) {
						System.out.println("enemie hits player!");
						listUsers.get(n).getPlayer().setCurHp(
								listUsers.get(n).getPlayer().getCurHp() - 10);
						hitPlayer = true;
						break;
					}
				}
				if (!hitPlayer) {
					listEnemies.get(i).setxPosition(x);
					listEnemies.get(i).setyPosition(y);
				}
			}
		}
		return listUsers;
	}

	public int getRoomNumber() {
		return roomNumber;
	}

	public LinkedList<Item> getListItems() {
		return listItems;
	}

	public void setListItems(LinkedList<Item> listItems) {
		this.listItems = listItems;
	}

}
